/**
 * @Author: LiMeng
 * @Date: 2017/4/27
 */
var GLOBAL = {
    YEARS: []
};
var getRecentTwentyYearsComboList = function () {
    var thisYear = new Date().getFullYear();
    for (var i = thisYear + 10; i > thisYear - 10; i--) {
        GLOBAL.YEARS.push({
            value: i,
            text: i
        });
    }
};
var getSelectedTeacher = function (data) {
    var names = '';
    var ids = '';
    for (var index in data) {
        names += ',' + data[index].realName;
        ids += ',' + data[index].id;
    }

    $('#teacherName').textbox('setValue', names.substring(1));
    $('#teacherId').val(ids.substring(1));
};
var getSelectedAddUser = function (data) {
    var names = '';
    var ids = '';
    for (var index in data) {
        names += ',' + data[index].realName;
        ids += ',' + data[index].id;
    }

    $('#addUserName').textbox('setValue', names.substring(1));
    $('#addUserId').val(ids.substring(1));
};
var openSelectUser = function (title, callback) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['900px', '500px'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml',
        callBack: callback
    });
};
var add_staffTrain_func = function () {
    parent.layer.open({
        type: 2,
        title: '添加施工人员培训记录',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/contractor/staffTrain/add.shtml?resourceId=' + $.getQueryParam('resourceId')
    });
};
var edit_staffTrain_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个施工人员培训记录！');
        return false;
    }

    parent.layer.open({
        type: 2,
        title: '编辑施工人员培训记录',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/contractor/staffTrain/edit.shtml?'
        + 'resourceId=' + $.getQueryParam('resourceId')
        + '&id=' + $selected[0].id
        + '&state=edit'
    });
};
var copy_staffTrain_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个施工人员培训记录！');
        return false;
    }

    parent.layer.open({
        type: 2,
        title: '添加施工人员培训记录',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/contractor/staffTrain/copy.shtml?'
        + 'resourceId=' + $.getQueryParam('resourceId')
        + '&id=' + $selected[0].id
        + '&state=copy'
    });
};
var view_staffTrain_func = function (id) {
    parent.layer.open({
        type: 2,
        title: '查看施工人员培训记录',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/contractor/staffTrain/view.shtml?'
        + 'resourceId=' + $.getQueryParam('resourceId')
        + '&id=' + id
        + '&state=view'
    });
};
var delete_staffTrain_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个施工人员培训记录！');
        return false;
    }

    var index = parent.layer.confirm('是否确定删除？', function () {
        $.ajax({
            type: "POST",
            url: ROOT_PATH + '/contractor/staffTrain/delete.shtml',
            data: {id: $selected[0].id},
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success) {
                    parent.layer.msg(result.message, {time: 1000, icon: 1}, function () {
                        $('#dg1').datagrid('load');
                        parent.layer.close(index); //先得到当前iframe层的索引
                    });
                } else {
                    parent.layer.msg(result.message);
                }
            }
        });
    });
};
$(function () {
    getRecentTwentyYearsComboList();
    $('#trainTypeId').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.CONTRACTOR_TRAIN_TYPE,
        valueField: 'id',
        textField: 'text',
        editable: false
    });
    $('#year').combobox({
        data: GLOBAL.YEARS,
        editable: false
    });
    $('#teacherName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择授课人', getSelectedTeacher);
            }
        }],
        editable: false
    });
    $('#addUserName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择登记人', getSelectedAddUser);
            }
        }],
        editable: false
    });
    $('#addDeptId').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        editable: false
    });
    //初始化加载datagrid
    $('#dg1').datagrid({
        method: 'post',
        url: ROOT_PATH + '/contractor/staffTrain/list.shtml',
        queryParams: {
            sort: 'addTime',
            order: 'desc'
        },
        toolbar: '#toolbar',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        sortOrder: true,
        striped: true,
        fit: true,
        columns: [[
            {field: 'ckid', checkbox: true},
            {
                field: 'name', title: '培训班名称', width: 100, formatter: function (value, row) {
                return '<a href="javascript:void(0)" onclick="view_staffTrain_func(\'' + row.id + '\')" class="easyui-linkbutton text-primary" plain="true">' + value + '</a>';
            }
            },
            {field: 'trainTypeName', title: '培训类别', width: 60},
            {field: 'trainNum', title: '培训人数', width: 50},
            {field: 'qualifiedNum', title: '合格人数', width: 50},
            {field: 'trainStartTime', title: '培训开始日期', width: 100},
            {field: 'trainEndTime', title: '培训结束日期', width: 100},
            {field: 'actualTrainHour', title: '培训学时', width: 50},
            {
                field: 'teacherList', title: '授课人', width: 100, formatter: function (value, row) {
                var str = '';
                if ($.isArray(value)) {
                    for (var index in value) {
                        str += ',' + value[index].realName;
                    }
                    str = '企内：' + str.substring(1) + ' ';
                }
                if (row.teacherNameOutside !== null && row.teacherNameOutside !== '') {
                    str = str + '企外：' + row.teacherNameOutside;
                }
                return str;
            }
            },
            {field: 'year', title: '年度', width: 50},
            {field: 'addUserId', title: '登记人id', hidden: true},
            {field: 'addUserName', title: '登记人', width: 60},
            {field: 'addDeptId', title: '登记部门id', hidden: true},
            {field: 'addDeptName', title: '登记部门', width: 100},
            {field: 'addTime', title: '登记时间', width: 100, sortable: true}
        ]]
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        $('#tb2').form('reset').find(':hidden').val('');
    });
    $('#btn_search').on("click", function () {
        $('#dg1').datagrid('reload', $('#tb1,#tb2').find(':input').serializeJson());
    });
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    $('#btn_staffTrain_add').on('click', function () {
        add_staffTrain_func();
    });
    $('#btn_staffTrain_edit').on('click', function () {
        edit_staffTrain_func();
    });
    $('#btn_staffTrain_delete').on('click', function () {
        delete_staffTrain_func();
    });
    $('#btn_staffTrain_copy').on('click', function () {
        copy_staffTrain_func();
    });
});